home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 361_01 / kbqgad.c < prev    next >
C/C++ Source or Header  |  1991-09-18  |  3KB  |  97 lines

  1.  
  2. /* KbqGad.c ---> Keyboard Queue Gadgets.
  3.  *
  4.  * Kbq_x() are taken from "Controlling The Keyboard Buffer" by Steven Gruel
  5.  * (The C Users Journal 7/90, pgs 85-6) w/ modifactions to return Keys.h
  6.  * values for Extented Characters.  These can be used in TSRs, unlike the 
  7.  * routines in <conio.h>.
  8.  *
  9.  * Get..() routines were suggested by similar routines in CUG-273.
  10.  *
  11.  * Author: J.Ekwall                                     13 September 91
  12.  *
  13.  * Copyrighted to the Public Domain.  Unlimited Distribution Authorized.
  14.  *
  15.  * User Assumes All Risks/Liabilities.
  16.  *
  17.  * Last Update: 13 September 91/EK
  18.  */
  19.  
  20. #include <stdek.h>
  21. #include <gadgets.h>
  22. #include <stdio.h>
  23. #include <dos.h>
  24.  
  25. #define KBQSEG  0x40
  26. #define KBQRD   0x1A
  27. #define KBQWRT  0x1C
  28. #define KBQBTM  0x1E
  29. #define KBQTOP  0x3E
  30.  
  31. int GetaKey(unsigned char *List, int CaseLess)
  32. /* Wait until one of the charaacters in the list (or Esc) is pressed. */
  33. {
  34.     int c;
  35.     
  36.     if (CaseLess) strupr(List);
  37.     do {
  38.        c = Kbq_read(); if (CaseLess) c = toupper(c);
  39.     } while (c != 27 && strchr(List, c) == NULL);
  40.     return c;
  41. }
  42.  
  43. unsigned int GetxKey(void)
  44. /* return an ascii value or 256 * ScanCode */
  45. {
  46.     union REGS rg;
  47.  
  48.     while (1) {
  49.        rg.h.ah = 1; int86(0x16, &rg, &rg);
  50.        if (rg.x.flags & 0x40) { int86(0x28, &rg, &rg); continue; }
  51.        rg.h.ah = 0; int86(0x16, &rg, &rg);
  52.        if (rg.h.al == 0) return rg.h.ah << 8; else return rg.h.al;
  53.     }
  54. }
  55.  
  56. void Kbq_flush(void) { poke(KBQSEG, KBQWRT, peek(KBQSEG, KBQRD)); }
  57.  
  58. int Kbq_poll(void)
  59. {                       /* Returns chr, Keys.h code     or Zero.  No Wait. */
  60.     if (peek(KBQSEG, KBQWRT) == peek(KBQSEG,KBQRD)) return 0;
  61.     return Kbq_read();
  62. }
  63.  
  64. int Kbq_snoop(int Which1)
  65. {         /* Report the "Nth" Keystroke in Queue.  (w/o Removal). */
  66.     int i, Fill, Empty, Keystroke;
  67.  
  68.     Fill = peek(KBQSEG, KBQWRT);  Empty = peek(KBQSEG,KBQRD);
  69.     if (Fill == Empty) return 0;
  70.     if ((i = Fill - Empty) < 0) i += 32; if ((Which1 *= 2) > i) return 0;
  71.     if ((i = Empty + Which1 - 2) >= KBQTOP) i -= 32;
  72.     Keystroke = peek(KBQSEG, i);
  73.     if ((Keystroke & 127) == 0) return ((Keystroke >> 8) | 128);
  74.     return (Keystroke & 127);
  75. }
  76.  
  77. int Kbq_stuff(unsigned char ch)
  78. {                               /* Shove a Keystroke    Into Keyboard Queue. */
  79.     int Fill, KeyStroke;
  80.  
  81.     if (!ch) return 0;
  82.     if (ch < 128) KeyStroke = ch; else KeyStroke = (ch & 127) << 8;
  83.     Fill = peek(KBQSEG, KBQWRT); poke(KBQSEG, Fill, KeyStroke);
  84.     if ((Fill += 2) >= KBQTOP) Fill = KBQBTM;
  85.     if (Fill == peek(KBQSEG,KBQRD)) return 0;   /* Full */
  86.     poke(KBQSEG, KBQWRT, Fill); return 1;
  87. }
  88.  
  89. int Kbq_tally(void)
  90. {       /* Report Number of Keystrokes in Queue. */
  91.     int i;
  92.  
  93.     i = (peek(KBQSEG, KBQWRT) - peek(KBQSEG,KBQRD)) / 2;
  94.     if (i < 0) return i + 16;  else return i;
  95. }
  96.  
  97.